Completed
Push — master ( cdc981...b2a603 )
by greg
01:49
created

post-upload.js ➔ ... ➔ req.busboy.file   C

Complexity

Conditions 12
Paths 3

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
c 1
b 0
f 0
nc 3
dl 0
loc 40
rs 5.1612
nop 5

How to fix   Complexity   

Complexity

Complex classes like post-upload.js ➔ ... ➔ req.busboy.file often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
import path from 'path'
2
import fse from 'fs-extra'
3
import mkdirp from 'mkdirp'
4
import limax from 'limax'
5
6
import {
7
  config,
8
  abeExtend
9
} from '../../cli'
10
11
var route = function(req, res, next){
12
  abeExtend.hooks.instance.trigger('beforeRoute', req, res, next)
13
  if(typeof res._header !== 'undefined' && res._header !== null) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
14
15
  var resp = {success: 1}
16
  var filePath
17
  var folderWebPath = '/' + config.upload.image
18
  folderWebPath = abeExtend.hooks.instance.trigger('beforeSaveImage', folderWebPath, req)
19
  var folderFilePath = path.join(config.root, config.publish.url, folderWebPath)
20
  mkdirp.sync(folderFilePath)
21
  req.pipe(req.busboy)
22
  req.busboy.on('file', function (fieldname, file, filename, encoding, mimetype) {
23
    var ext = path.extname(filename).toLowerCase()
24
    var filenameNoExt = path.basename(filename,ext)
25
    var randID = '-' + (((1+Math.random())*0x100000)|0).toString(16).substring(2)
26
    var slug = limax(filenameNoExt, {separateNumbers: false}) + randID + ext
27
    var hasSentHeader = false
28
    
29
    filePath = path.join(folderFilePath, slug)
30
    resp['filePath'] = path.join(folderWebPath, slug)
31
32
    var returnErr = function (msg) {
33
      hasSentHeader = true
34
      file.resume()
35
      res.set('Content-Type', 'application/json')
36
      res.send(JSON.stringify({error: 1, response: msg}))
37
    }
38
39
    file.on('limit', function() {
40
      returnErr('file is too big')
41
    })
42
43
    if (mimetype !== 'image/gif' && mimetype !== 'image/jpeg' && mimetype !== 'image/png' && mimetype !== 'image/svg+xml' && mimetype !== 'video/mp4') {
44
      returnErr('unauthorized file')
45
    } else if (ext !== '.gif' && ext !== '.jpg' && ext !== '.jpeg' && ext !== '.png' && ext !== '.svg' && ext !== '.mp4') {
46
      returnErr('not a valid asset')
47
    }
48
49
    var fstream = fse.createWriteStream(filePath)
50
51
    fstream.on('finish', function() {
52
      if(hasSentHeader) return
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
53
      if(/\.(jpg|png|gif|svg)/.test(filePath)){
54
        resp = abeExtend.hooks.instance.trigger('afterSaveImage', resp, req)
55
      }
56
      res.set('Content-Type', 'application/json')
57
      res.send(JSON.stringify(resp))
58
    })
59
60
    file.pipe(fstream)
61
  })
62
}
63
64
export default route